<%@ page language="java" import="java.util.*,cat.beans.*,cat.dao.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML>
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title></title>
    
    
   
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/maintable.css" ></link>
	<style type="text/css">
		/*提交按钮的样式*/
		.submitbutton{
			background:url(images/btn_bg.jpg) repeat;
			border:1px solid #8FB9D0;
			font-size:12px;
			margin-left:80px;
			margin-top:15px;
			line-height:14px;
			color:#295568;
			cursor:pointer;
		}

		.submitbutton:hover{
			color:red;  /*ie6对此属性无效*/
		}
		
		.firsttd{
			width:30%;
		}
		
	</style>
	<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.8.0.js"></script>

	<%
		//查菜单表,把菜单数据查询出来
		List<MenuInfo> menuList = new MenuDao().getMenuList(0);
		request.setAttribute("menuList", menuList);
		int adminId=Integer.parseInt(request.getParameter("id"));
		String permStr=new PermDao().getPermIdList(adminId);  //查询被选中用户有哪些菜单id,返回一个字符串 例如  3,4,5,2
	%>
	
  </head>
  
  <body>
    <div class ="div_title">
			<div class="div_titlename"> <img src="images/san_jiao.gif" ><span>用户权限管理</span></div>
			<div class="div_titleoper"><input type="checkbox"  id="ckall" /> 全选 </div>	
	</div>
  	<form action="PermServlet.do" method="post">
  		<input type="hidden" name="flag" value="updatperm" />
  		<input type="hidden" name="id"  value="${param.id }"/>
	   	<table class="main_table">
	  	 	<tr>
	   			<th class="firsttd">一级菜单</th><th>二级菜单</th>
	   		</tr>
	   		<c:forEach var="m" items="${menuList }">
	   			<tr>
	   				<td>
	   					<input type="checkbox" name="menuIds" id="${m.id }"  value="${m.id }"  onclick="checkSubItem(this)"/>  ${m.menuName }
	   				</td>
	   				<td>
	   					<c:forEach  var="m_sub" items="${m.subMenuList }">
	   						<input type="checkbox" class="${m.id}" name="menuIds"  value="${m_sub.id }" onclick="checkParent(${m.id})"/> ${m_sub.menuName } <br />
	   					</c:forEach>
	   				</td>
	   			</tr>
	   		</c:forEach>	  		
	   	</table>

	   	<input type="submit" class="submitbutton" onclick="return confirm('确定提交吗')" value="保存修改" />
	   
   	</form>
	
   	<script>
   		var permStr='<%=permStr%>'; 
   		
   		var permArray=permStr.split(",");  //把权限id字符串转成数组
   		
   		//给数组动态添加一个contains方法
   		Array.prototype.contains=function(e){
   			for(var i=0;i<this.length;i++){
   				if(this[i]==e){
   					return true;
   				}
   			}
   			
   			return false;
   		};
   		
   		//取到所有的复选框,进行循环
   		$("input[type=checkbox]").each(function(){
   			if(permArray.contains(this.value)){
   				this.checked=true;
   			}
   		});
   		
   		//勾选父级复选框,让子项全选中或取消,注意,子项中加了一个class属性 ,值为父级checkbox的 id,用这样的方式建立起来的关联
   		function checkSubItem(txtMenu){
   			$("."+txtMenu.id).attr("checked",txtMenu.checked);
   		}
   		
   		//勾选子级复选框后,处理父级复选框的状态 ,传过来的参数值是父级复选框的id
   		function checkParent(parentId){
   			$("#"+parentId).removeAttr("checked");
   			
   			$("."+parentId).each(function(){
   				if(this.checked){
   					$("#"+parentId).attr("checked","checked");
   					return;
   				}
   			});	
   		}
   		
   	</script>
   	
   	<script>
   		var msg="${msg}";
   		if(msg!=""){
   			alert(msg);
   		}
   	</script>
   	
   	<script>
   		$(function(){
   		    //处理全选
   			$("#ckall").click(function(){
   				if($("#ckall").attr("checked")=="checked"){
   					$("input[name=menuIds]").attr("checked","checked");
   				}
   				else{
   					$("input[name=menuIds]").removeAttr("checked");
   				}
   			
   			});
   			
   			//如果有不选的.也可把全选框勾掉,如果全选了,也要把全选框自动勾上
   			$("input[name=menuIds]").click(function(){
   				if($("input[name=menuIds]").not("input:checked").size()>0) {  //如果有没有勾选的
   					$("#ckall").removeAttr("checked");
   				}
   				else{
   					$("#ckall").attr("checked","checked");
   				}
   			});
   			
   			//页面加载的时候,如果权限是全的,也应该将全选勾上
   			if($("input[name=menuIds]").not("input:checked").size()==0) {  //如果有没有勾选的
   				$("#ckall").attr("checked","checked");
   			}
   			
   		});
   	</script>
  </body>
</html>
